﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using SWEN_HMS.Classes;

namespace SWEN_HMS.BookingModule
{
    public partial class BookingMainPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataSource1.SelectCommand = "Select bookingID From Booking Where checkInStatus Like'" + "False" + "%'";

            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
        }

        protected void Create_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/BookingModule/RoomBooking.aspx");
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 2;
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
        }

        protected void roomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ArrayList roomNo = RoomDBManager.getDetailByRoomType(roomType.SelectedValue);
            newRoom.DataSource = roomNo;
            newRoom.DataBind();
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            checkInDate.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
        }

        //Calendar validation
        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            DateTime pastday = e.Day.Date;
            DateTime date = DateTime.Now;
            int year = date.Year;
            int month = date.Month;
            int day = date.Day;
            DateTime today = new DateTime(year, month, day);
            if (pastday.CompareTo(today) < 0)
            {
                e.Cell.BackColor = System.Drawing.Color.Gray;
                e.Day.IsSelectable = false;
            }
        }

        protected void countCOD_Click(object sender, EventArgs e)
        {
            double day = double.Parse(durationOfStay.Text);
            DateTime checkin = Calendar1.SelectedDate.AddDays(day - 1);
            checkOutDate.Visible = true;
            checkOutDate.Text = checkin.ToString("dd/MM/yyyy");
        }

        protected void tbxUpdate_Click(object sender, EventArgs e)
        {
            Booking b = new Booking();
            int row = 0;

            b.bookingID = Convert.ToInt32(bookingIDDDL.SelectedValue);
            b.roomType = roomType.SelectedValue;
            b.roomNo = Convert.ToInt32(newRoom.SelectedValue);
            b.noOfAdult = Convert.ToInt32(noAdult.Text);
            b.noOfChild = Convert.ToInt32(noChild.Text);
            b.checkIn = checkInDate.Text;
            b.checkOut = checkOutDate.Text;
            b.noOfDay = Convert.ToInt32(durationOfStay.Text);


            //switch the guest to the new room that has been changed
            row = RoomDBManager.updateRoomBookingInformation(b);
            if (row > 0)
            {
                //set the previous room to Available
                Room rm = new Room();
                rm.roomNo = Convert.ToInt32(oldRoom.Text);
                rm.roomStatus = "Available";
                rm.noOfAdult = 0;
                rm.noOfChild = 0;
                RoomDBManager.updateRoomStatus(rm);

                //set the new room to Occupied
                Room r = new Room();
                r.roomNo = Convert.ToInt32(newRoom.SelectedValue);
                r.roomStatus = "Occupied";
                r.noOfAdult = Convert.ToInt32(noAdult.Text);
                r.noOfChild = Convert.ToInt32(noChild.Text);
                RoomDBManager.updateRoomStatus(r);

                System.Windows.Forms.MessageBox.Show("Update Successful!");
                Response.Redirect("~/BookingModule/RoomAvailability.aspx");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Update Unsuccessful!");
            }
        }

        protected void search_Click(object sender, EventArgs e)
        {
            Booking store = RoomDBManager.getDetailByBookingID(Convert.ToInt32(bookingIDDDL.SelectedValue));
            oldRoom.Text = store.roomNo.ToString();
            noAdult.Text = store.noOfAdult.ToString();
            noChild.Text = store.noOfChild.ToString();
        }

        protected void cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/BookingModule/RoomAvailability.aspx");
        }

        protected void search2_Click(object sender, EventArgs e)
        {
            Panel2.Visible = true;
            Booking b = RoomDBManager.getDetailByBookingID(Convert.ToInt32(bookingIDDDL2.SelectedValue));
            bookingID.Text = Convert.ToString(b.bookingID);
            checkInD.Text = Convert.ToDateTime(b.checkIn).ToShortDateString();
            checkOutD.Text = Convert.ToDateTime(b.checkOut).ToShortDateString();
            roomNo.Text = Convert.ToString(b.roomNo);
            first.Text = b.firstName;
            last.Text = b.lastName;
        }

        protected void deleteBooking_Click(object sender, EventArgs e)
        {
            RoomDBManager.deleteBooking(Convert.ToInt32(bookingID.Text));
            RoomDBManager.updateRoomAvailability(Convert.ToInt32(roomNo.Text));
            System.Windows.Forms.MessageBox.Show("Delete Successful!");
            Response.Redirect("~/BookingModule/RoomAvailability.aspx");
        }

    }
}